package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go-zero-example/cmd/userapi/internal/svc"
	"go-zero-example/cmd/userapi/internal/types"
	"go-zero-example/cmd/userrpc/pb/userrpc"
)

type UserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {
	return &UserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Register 用户注册
func (l *UserLogic) Register(req *types.RegisterUserRequest) (resp *types.RegisterUserResponse, err error) {

	resp = new(types.RegisterUserResponse)
	// 调用rpc
	userResp, err := l.svcCtx.UserRpc.Register(l.ctx, &userrpc.RegisterUserRequest{
		Username: req.UserName,
		Phone:    req.Phone,
	})
	if err != nil {
		return nil, err
	}
	resp.UserId = userResp.UserId
	resp.Message = userResp.Message
	return resp, nil
}

// GetUser 获取用户信息
func (l *UserLogic) GetUser(req *types.GetUserRequest) (resp *types.GetUserResponse, err error) {

	resp = new(types.GetUserResponse)
	// 调用rpc
	userResp, err := l.svcCtx.UserRpc.GetUser(l.ctx, &userrpc.GetUserRequest{
		Userid: req.UserId,
	})
	resp.User = userResp.User
	resp.Message = userResp.Message
	return resp, nil
}

// RePhone 重置手机号
func (l *UserLogic) RePhone(req *types.RePhoneUserRequest) (resp *types.RePhoneUserResponse, err error) {

	//resp.Message = "注册成功"
	resp = new(types.RePhoneUserResponse)
	// 调用rpc
	userResp, err := l.svcCtx.UserRpc.RePhone(l.ctx, &userrpc.RePhoneRequest{
		UserId: req.UserId,
		Phone:  req.Phone,
	})
	resp.Message = userResp.Message
	return resp, nil
}
